package com.ksd.api.controller;

import com.ksd.api.beans.*;
import com.ksd.api.utils.HttpServletUtil;
import com.ksd.common.core.controller.BaseController;
import com.ksd.common.core.domain.AjaxResult;
import com.ksd.common.utils.DateUtils;
import com.ksd.common.utils.StringUtils;
import com.ksd.system.domain.*;
import com.ksd.system.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


@RestController
public class SearchSongList extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(SearchSongList.class);
    @Autowired
    private ISysMusicDataService sysMusicDataService;
    @Autowired
    private ISysBoardService sysBoardService;

    @RequestMapping(value = "/api/user/vod/searchSongList", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult getVodList(HttpServletRequest httpServletRequest) {
        logger.info("===================关键字分页搜索歌曲===============");
        StringBuffer requestURL = httpServletRequest.getRequestURL();

        HttpServletUtil.getRequestParam(httpServletRequest);
        String typeDataId = httpServletRequest.getParameter("typeDataId");
        String pageSize = httpServletRequest.getParameter("pageSize");
        String keyword = httpServletRequest.getParameter("keyword");
        String cid = httpServletRequest.getParameter("cat_id");
        String pageNum = httpServletRequest.getParameter("pageNum");

        SysMusicData sysMusicData = new SysMusicData();
        if (StringUtils.isNotEmpty(keyword)) {
            sysMusicData.setKeyWord(keyword);
        }
        if (StringUtils.isNotEmpty(cid)) {
            sysMusicData.setAlbumId(Long.valueOf(cid));
        }
        SysBoard sysBoard = sysBoardService.selectSysBoardById(SysBoard.serviceFileId);
        String serviceIpUrl = sysBoard.getUrl();

        startPage();
        List<SysMusicData> sysMusicDataList = sysMusicDataService.selectSysMusicDataList(sysMusicData);
        logger.info("====不进行分页,直接获取所有音乐个数======="+sysMusicDataList.size()+"========cid:"+cid);
        List<SongAddress> songAddressList = new ArrayList<>();
        for (SysMusicData result : sysMusicDataList) {
            SongAddress songAddress = new SongAddress();
            songAddress.setId(result.getId());
            songAddress.setNum(1);
            songAddress.setName(result.getTitle());
            songAddress.setUrl_address(serviceIpUrl+result.getMusicUrl());
            songAddress.setUrlAddress(serviceIpUrl+result.getMusicUrl());
            songAddress.setSinger(result.getSinger());
            songAddress.setImg_address(serviceIpUrl+result.getImgUrl());
            songAddress.setImage_address(serviceIpUrl+result.getImgUrl());
            songAddress.setImgAddress(serviceIpUrl+result.getImgUrl());
            songAddress.setImageAddress(serviceIpUrl+result.getImgUrl());
            songAddress.setStatus(0);
            songAddress.setCreated_at(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,result.getCreateTime()));
            songAddress.setTitle(result.getTitle());
            songAddress.setFiles_type("music");
            songAddress.setType("group");
            songAddress.setSong_word(result.getSongWord());
            songAddressList.add(songAddress);
        }
        return AjaxResult.success(songAddressList);
    }

}